<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-05-17 16:04:33
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-06-06 08:51:13
*/
//商品控制器
class GoodsControl extends AuthControl{
	private $model;
	public function __auto(){
		$this->model=K('Goods');
	}
	//栏目列表Ajax返回
	public function selectCate(){
		$cate=cache('categoryCache');
		$html="";
		foreach ($cate as $k => $v) {
			$html.="<option value='".$v['cid']."'>".$v['_name']."</option>";
		}
		// echo json_encode($html);die;
		echo $html;die;
	}
	//首页列表
	public function index(){
		$total=$this->model->count();
		$page = new page($total);
		$fields = '*,hd_g_goods.keywords,hd_g_goods.description';
		$this->goods = $this->model->field($fields)->order('gid DESC')->limit($page->limit())->all();
		$this->page=$page->show();
		$this->display();
	}
	//添加商品
	public function add(){
		if(IS_POST){
			// p($_POST);die;
			if($this->model->addGoods()){
				$this->success('添加商品成功');
			}else{
				$this->error($this->model->error);
			}
		}else{
			$cid = Q('cid',0,'intval');
			$cate = cache('categoryCache');
			$this->cate = $cate[$cid];
			$gtid = $cate[$cid]['gtid'];
			//读取商品属性表单
			$gt = K('GoodsType');
			$aclass = $gt->get($gtid);
			//读取商品规格属性表单
			$gt = K('GoodsSpec');
			$aspec = $gt->get($gtid);
			//读取品牌
			$this->brand=M('brand')->where(array('cid'=>$cid))->all();

			$this->aclass=$aclass;
			$this->aspec=$aspec;
			$this->display();
		}
	}
	//编辑商品
	public function edit(){
		if(IS_POST){
			// p($_POST);
			if($this->model->editGoods()){
				$this->success('修改成功...',U('index'));
			}else{
				$this->error($this->model->error);
			}
		}else{
			$gid = Q('get.gid',0,'intval');
			$fields = '*,hd_g_goods.keywords,hd_g_goods.description';
			$field = $this->model->field($fields)->find($gid);
			// p($field);
			$cid = $field['cid'];
			//读取图片
			$pic = M('goods_pic')->where(array('gid'=>$gid))->all();
			$this->pic=$pic;
			$this->path = ROOT_PATH;
			//读取分类
			$cate = cache('categoryCache');
			$this->cate = $cate[$cid];
			//读取品牌
			$this->brand=M('brand')->where(array('cid'=>$cid))->all();
			$this->field = $field;
			//读取商品属性
			$gtid = $cate[$cid]['gtid'];
			$gt = K('GoodsType');
			// $gtdata = M('goods_attr')->where('gid='.$gid)->all();
			$gtdata = M('goods_attr')->where('gid='.$gid)->getField('avid,avalue');
			$aclass = $gt->get($gtid,$gtdata);
			$this->aclass=$aclass;
			//读取商品规格属性表单
				//读取库存列表
				$stock = M('stock')->where('gid='.$gid)->all();
				$this->stock=$stock;
				//读取库存属性列表
				$gtSpec = K('GoodsSpec');
				foreach ($stock as $v) {
					$gtdata=M('stock_attr')->where('stid='.$v['stid'])->field('avid')->all();
					$aspec[] = $gtSpec->get($gtid,$gtdata);
				}
				// p($aspec);
			$this->aspec=$aspec;

			$this->display();
		}
	}
	//删除商品
	public function del(){
		if($this->model->delGoods()){
			$this->success('删除成功...');
		}else{
			$this->error('删除失败...');
		}
	}
	//删除商品图片ajax
	public function delPic(){
		$pid = Q('pid',0,'intval');
		$db = M('goods_pic');
		$d = $db->find($pid);
		@unlink(ROOT_PATH.$d['small']);
		@unlink(ROOT_PATH.$d['medium']);
		@unlink(ROOT_PATH.$d['big']);
		$id = $db->del($pid);
		echo $id;exit;
	}
	//更新缓存
	public function updateCache(){
		if($this->model->updateCache()){
			$this->success('缓存更新成功');
		}
	}
}
